package cn.apple.org.service.impi;

import cn.apple.basic.utils.PageList;
import cn.apple.org.domain.Department;
import cn.apple.org.mapper.DepartmentMapper;
import cn.apple.org.query.DepartmentQuery;
import cn.apple.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service // 交给spring管理
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void add(Department department) {
        departmentMapper.add(department);


    }

    @Override
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    public void delete(Long id) {
        departmentMapper.delete(id);
    }

    @Override
    public Department findById(Long id) {
        return departmentMapper.findById(id);
    }

    @Override
    public List<Department> findAll() {
        return departmentMapper.findAll();
    }

    /**
     * 查询 分页 totals data
     *
     * @param query
     * @return
     */
    @Override
    public PageList<Department> queryByPage(DepartmentQuery query) {
        //  查询总数
        Integer count = departmentMapper.queryByCount(query);
        System.out.println(count);
        //  如果总数为0或者null，返回一个空PageList对象
        if (0 == count || null == count) {
            return new PageList<>(0, new ArrayList<>());
        }
        //  查询数据并封装到List中
        List<Department> departments = departmentMapper.queryByPage(query);
        //  返回一个新的PageList对象，包含总数和查询到的数据
        return new PageList<>(count, departments);
    }
}
